<?php
function findage($dob)
{
    $localtime = getdate();
    $today = $localtime['mday']."-".$localtime['mon']."-".$localtime['year'];
    $dob_a = explode("-", $dob);
    $today_a = explode("-", $today);
    $dob_d = $dob_a[0];
	$dob_m = $dob_a[1];
	$dob_y = $dob_a[2];
    $today_d = $today_a[0];$today_m = $today_a[1];$today_y = $today_a[2];
    $years = $today_y - $dob_y;
    $months = $today_m - $dob_m;
    if ($today_m.$today_d < $dob_m.$dob_d) 
    {
        $years--;
        $months = 12 + $today_m - $dob_m;
    }

    if ($today_d < $dob_d) 
    {
        $months--;
    }

    $firstMonths=array(1,3,5,7,8,10,12);
    $secondMonths=array(4,6,9,11);
    $thirdMonths=array(2);

    if($today_m - $dob_m == 1) 
    {
        if(in_array($dob_m, $firstMonths)) 
        {
            array_push($firstMonths, 0);
        }
        elseif(in_array($dob_m, $secondMonths)) 
        {
            array_push($secondMonths, 0);
        }elseif(in_array($dob_m, $thirdMonths)) 
        {
            array_push($thirdMonths, 0);
        }
    }
    return "$years Years $months months.";
}
//debug($routes);
?>


<!-- Job Section -->
			<div id="job">
				<div class="toolbar">
					<h1>Job Details</h1>
					<a class="back" href="#home">Back</a>
				</div>
				<h2>Job / Request</h2>
				<ul class="rounded">
					<?php 
					
					foreach ($jobs as $job)
					{
					echo '<li class="">#'.$job[0]['JobId'].' '.$job[0]['JobName'].'</li>';
					echo '<li class="">'.$job[0]['JobType'].' - '.$job[0]['Description'].'</li>';
					echo '<li class="">Ver - '.$job[0]['RequestVersion'].'</li>';
					echo '<li class="">'.date_format(date_create_from_format('Y-m-d H:i:s.u', $job[0]['StartDate']), 'd-m-y').' to '.date_format(date_create_from_format('Y-m-d H:i:s.u', $job[0]['EndDate']), 'd-m-y').'</li>';
					}
					
				?>
				</ul>
				
					<?php foreach($clients as $client)
					{
						echo '<h2>Client</h2>';
						echo '<ul class="rounded">';
						echo '<li>'.$client[0]['FirstName'].' '.$client[0]['Surname'].' '.findAge(date('d-m-Y', strtotime($client[0]['DOB']))).'</li>';
						if(isset($client[0]['RequiresRFCarSeatBefore']))
						{
							echo'<li>Requires Rear Facing Car Seat</li>';
						}
						else if(isset($client[0]['RequiresFFCarSeatBefore']))
						{
							echo'<li>Requires Front Facing Car Seat</li>';
						}
						else if(isset($client[0]['RequiresBoosterSeatBefore']))
						{
							echo'<li>Requires Booster Seat</li>';
						}
						echo '</ul>';
					}
				?>
				
				
				<?php


					foreach($staffs as $key => $staff)
					{
						echo '<h2>Route for '.$staff[0]['FirstName'].' '.$staff[0]['Surname'].'</h2>';
						echo '<ul class="rounded">';
						
						$worker = preg_replace('/[^\p{L}\p{N}\s]/u', '', $staff[0]['FirstName']);

                        $check = count($$worker)-1; //counter
						foreach($$worker as $contactWorker)
						{


							if($contactWorker[0]['ContactLocationFlag'] == 'Y')
							{ $contactFlag = 'Yes'; }
							else
							{ $contactFlag = 'No'; }
									
							$address = $contactWorker[0]['AddressName'].' '.$contactWorker[0]['LevelNumber'].' '.$contactWorker[0]['UnitNumber'].' '.$contactWorker[0]['StreetNumber'].' '.$contactWorker[0]['StreetName'].' '.$contactWorker[0]['StreetSuffix'].' '.$contactWorker[0]['TownSuburb'].' '.$contactWorker[0]['State'].' '.$contactWorker[0]['PostCode'];
									echo '<li class="orange"><a href="#'.preg_replace('/[^\p{L}\p{N}\s]/u', '', $staff[0]['FirstName']).preg_replace('/[^\p{L}\p{N}\s]/u', '', $staff[0]['Surname']).preg_replace('/[^\p{L}\p{N}\s]/u', '', $contactWorker[0]['RoutePointSequence']).'">'.$contactWorker[0]['RoutePointSequence'];
									echo ' - '.$contactWorker[0]['RouteName'].'</a></li>';
							$route[$check] = $address; //added route array to include addresses in the foreach loop
							$check = $check-1; //decrementing count

						}
						// echo '<li class="orange"><a href="#'.preg_replace('/[^\p{L}\p{N}\s]/u', '', $staff[0]['FirstName']).preg_replace('/[^\p{L}\p{N}\s]/u', '', $staff[0]['Surname']).preg_replace('/[^\p{L}\p{N}\s]/u', '', "FullRoute").'">';
                        // echo 'Full Route</a></li>'; //line above and current line created seperate button for the Full Route option

						echo '</ul>';
					}

					?>

					<h2>Notes</h2>
					<ul class="rounded">
						<?php foreach($notes as $note)
							{
								echo '<li>'.$note[0]['NoteText'].' on '.date('d/m/Y', strtotime($note[0]['CreatedOn'])).'</li>';
							}
							
							?>
					</ul>
				</div>
				
						
			</div>
			
			<?php
			
			foreach($staffs as $key => $staff)
					{
						
						$worker = preg_replace('/[^\p{L}\p{N}\s]/u', '', $staff[0]['FirstName']);
						foreach($$worker as $contactWorker)
						{
							echo '<div id="'.preg_replace('/[^\p{L}\p{N}\s]/u', '', $staff[0]['FirstName']).preg_replace('/[^\p{L}\p{N}\s]/u', '', $staff[0]['Surname']).preg_replace('/[^\p{L}\p{N}\s]/u', '', $contactWorker[0]['RoutePointSequence']).'" class="selectable">';
							if($contactWorker[0]['ContactLocationFlag'] == 'Y')
							{ $contactFlag = 'Yes'; }
							else
							{ $contactFlag = 'No'; }
									
							$address = "";
							if($contactWorker[0]['LevelNumber']!= '')
							$address .= $contactWorker[0]['LevelNumber'].'- ';
							if($contactWorker[0]['UnitNumber']!= '')
							$address .= $contactWorker[0]['UnitNumber'].'/ ';
							if($contactWorker[0]['StreetNumber']!= '')
							$address .= $contactWorker[0]['StreetNumber'].' ';
							
							
							$address .= $contactWorker[0]['StreetName'].' '.$contactWorker[0]['StreetSuffix'].' '.$contactWorker[0]['TownSuburb'].' '.$contactWorker[0]['State'].' '.$contactWorker[0]['PostCode'].' '.$contactWorker[0]['Country'];
									echo '<p><center><strong>'.$contactWorker[0]['RoutePointSequence'];
									echo '<br />'.$contactWorker[0]['RouteName'].'</strong></center></p>';
									echo '<p><center><strong>Address:</strong><a href="http://maps.google.com/?q='.$address.'">'.$contactWorker[0]['AddressName'].' '.$address.'</a></center></p>';
									echo '<p><center>From '.date_format(date_create_from_format('Y-m-d H:i:s.u', $contactWorker[0]['ExpectedArrivalTime']),'H:i').' to '.date_format(date_create_from_format('Y-m-d H:i:s.u', $contactWorker[0]['ExpectedDepartureTime']), 'H:i').'</center></p>'; 
									echo '<p><center><strong>Contact Location: </strong>'.$contactFlag.'</center></p>';
									if(($contactWorker[0]['Note'] != "")) echo '<p><center><strong>Notes: </strong>'.$contactWorker[0]['Note'].'</center></p>';

									echo '<center><img border="0" class="map" src="http://maps.googleapis.com/maps/api/staticmap?center='.$contactWorker[0]['Latitude'].','.$contactWorker[0]['Longitude'].'&markers=color:red%7Ccolor:red%7Clabel:'.$contactWorker[0]['RoutePointSequence'].'%7C'.$contactWorker[0]['Latitude'].','.$contactWorker[0]['Longitude'].'&zoom=16&size=300x450&sensor=false" alt="'.$contactWorker[0]['RouteName'].'"></center>';

							echo '<p><br><br><a href="#" class="grayButton goback">Close</a></p>';
							echo '</div>';
						}
						
					}
			echo '<div id="'.preg_replace('/[^\p{L}\p{N}\s]/u', '', $staff[0]['FirstName']).preg_replace('/[^\p{L}\p{N}\s]/u', '', $staff[0]['Surname']).preg_replace('/[^\p{L}\p{N}\s]/u', '', "FullRoute").'" class="selectable">';
// line above created to display route map after user clicks it
                $check2 = count($route)-1;

               $saddr1 = trim($route[$check2]);
             /*  $count = strlen($saddr1);
                               $i = 0;
                               while( $i < $count ) {
                               	if( ctype_digit($saddr1[$i]) ) {
                               		$numpos = $i; //cycle through string until it finds a numeric value then exits while loop. Returns string position
                               		return;
                               	}
                               	$i++;

                }
                echo $numpos;
               $saddr2 = substr($saddr1, $numpos);
                */
               $saddr = str_replace(' ','+', $saddr1); //starting address


               $daddr1 = trim($route[0]);
              /* $count2 = strlen($daddr1);
                                              $p = 0;
               while( $p < $count2 ) {
                                              	if( ctype_digit($daddr1[$p]) ) {
                                              		$numpos2 = $p;
                                              		return;
                                              	}
                                              	$p++;
                }

                $daddr2 = substr($daddr1, $numpos2);
                */
               $daddr = str_replace(' ','+', $daddr1); //destination address

               echo ' <iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com.au/maps?saddr='.$saddr.'&daddr='.$daddr.'&amp;output=embed"></iframe>';



			?>
<!-- Scheldue Section END -->

